home *** CD-ROM | disk | FTP | other *** search
/ Macwelt 4 / Macwelt DVD 4.cdr / Entwickler / Mac-OS X / Pantomime / Source / URLName.m / URLName.m
Encoding:
Text File  |  2002-08-08  |  6.3 KB  |  288 lines

  1. /*
  2. **  URLName.m
  3. **
  4. **  Copyright (c) 2001, 2002
  5. **
  6. **  Author: Ludovic Marcotte <ludovic@Sophos.ca>
  7. **
  8. **  This library is free software; you can redistribute it and/or
  9. **  modify it under the terms of the GNU Lesser General Public
  10. **  License as published by the Free Software Foundation; either
  11. **  version 2.1 of the License, or (at your option) any later version.
  12. **  
  13. **  This library is distributed in the hope that it will be useful,
  14. **  but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. **  Lesser General Public License for more details.
  17. **  
  18. **  You should have received a copy of the GNU Lesser General Public
  19. **  License along with this library; if not, write to the Free Software
  20. **  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #import <Pantomime/URLName.h>
  23.  
  24. #import <Pantomime/Constants.h>
  25.  
  26. @implementation URLName
  27.  
  28. - (id) initWithString: (NSString *) theString
  29. {
  30.   return [self initWithString: theString
  31.            path: nil];
  32. }
  33.  
  34. - (id) initWithString: (NSString *) theString
  35.          path: (NSString *) thePath
  36. {
  37.   self = [super init];
  38.  
  39.   // We initialize our ivars
  40.   protocol = nil;
  41.   foldername = nil;
  42.   host = nil;
  43.   port = 0;
  44.   username = nil;
  45.   password = nil;
  46.  
  47.   path = thePath;
  48.   
  49.   if ( path )
  50.     {
  51.       RETAIN(path);
  52.     }
  53.  
  54.   // We now decode our URL
  55.   [self _decodeURL: theString];
  56.   
  57.   return self;
  58. }
  59.  
  60. - (void) dealloc
  61. {
  62.   TEST_RELEASE(protocol);
  63.   TEST_RELEASE(foldername);
  64.   TEST_RELEASE(path);
  65.   TEST_RELEASE(host);
  66.   TEST_RELEASE(username);
  67.   TEST_RELEASE(password);
  68.   
  69.   [super dealloc];
  70. }
  71.  
  72.  
  73. //
  74. // access/mutation methods
  75. //
  76. - (NSString *) protocol
  77. {
  78.   return protocol;
  79. }
  80.  
  81. - (NSString *) foldername
  82. {
  83.   return foldername;
  84. }
  85.  
  86. - (NSString *) path
  87. {
  88.   return path;
  89. }
  90.  
  91. - (NSString *) host;
  92. {
  93.   return host;
  94. }
  95.  
  96. - (int) port
  97. {
  98.   return port;
  99. }
  100.  
  101. - (NSString *) username
  102. {
  103.   return username;
  104. }
  105.  
  106. - (NSString *) password
  107. {
  108.   return password;
  109. }
  110.  
  111. - (NSString *) description
  112. {
  113.   return [NSString stringWithFormat: @"protocol = (%@), foldername = (%@), path = (%@), host = (%@), port = (%d), username = (%@), password = (%@)",
  114.            [self protocol], [self foldername], [self path], [self host], [self port], [self username], [self password]];
  115. }
  116.  
  117. - (NSString *) stringValue
  118. {
  119.   if ( [[self protocol] caseInsensitiveCompare: @"LOCAL"] == NSOrderedSame )
  120.     {
  121.       return [NSString stringWithFormat: @"local://%@/%@", [self path], [self foldername]];
  122.     }
  123.   else if  ( [[self protocol] caseInsensitiveCompare: @"IMAP"] == NSOrderedSame )
  124.     {
  125.       return [NSString stringWithFormat: @"imap://%@@%@/%@", [self username], [self host], [self foldername]];
  126.     }
  127.   else
  128.     {
  129.       return [NSString stringWithFormat: @"pop3://%@@%@", [self username], [self host]];
  130.     }
  131. }
  132.  
  133. @end
  134.  
  135.  
  136. //
  137. // Private methods
  138. //
  139. @implementation URLName (Private)
  140.  
  141. //// FIXME (finish!)
  142. // imap://<iserver>/<foldername>
  143. //
  144. // Examples: imap://minbari.org/gray-council;UIDVALIDITY=385759045/;UID=20
  145. //           imap://michael@minbari.org/users.*;type=list
  146. //           imap://psicorp.org/~peter/%E6%97%A5%E6%9C%AC%E8%AA%9E/
  147. //           imap://;AUTH=KERBEROS_V4@minbari.org/gray-council/;uid=20/;section=1.2
  148. //           imap://;AUTH=*@minbari.org/gray%20council?SUBJECT%20shadows
  149. //
  150. // Note: The imap:// part isn't present in the received string as parameter.
  151. //
  152. - (void) _decodeIMAP: (NSString *) theString
  153. {
  154.   NSRange r1, r2;
  155.  
  156.   // We decode the username
  157.   r1 = [theString rangeOfString: @"@"
  158.           options: NSBackwardsSearch];
  159.   
  160.   if ( r1.length )
  161.     {
  162.       username = [theString substringToIndex: r1.location];
  163.       RETAIN(username);
  164.     }
  165.   else
  166.     {
  167.       r1.location = 0;
  168.     }
  169.   
  170.   r2 = [theString rangeOfString: @"/"
  171.           options: 0
  172.           range: NSMakeRange(r1.location, [theString length] - r1.location)];
  173.   
  174.   if ( r1.length )
  175.     {
  176.       host = [theString substringWithRange: NSMakeRange(r1.location + 1, r2.location - r1.location - 1)];
  177.     }
  178.   else
  179.     {
  180.       host = [theString substringWithRange: NSMakeRange(r1.location, r2.location - r1.location)];
  181.     }
  182.   
  183.   RETAIN(host);
  184.  
  185.   foldername = [theString substringFromIndex: (r2.location + 1)];
  186.   RETAIN(foldername);
  187.  
  188.   //NSDebugLog(@"|%@| |%@| |%@|", username, host, foldername);
  189. }
  190.  
  191.  
  192. //
  193. // local://<path>/<foldername> (full path)
  194. //
  195. // Note: The local:// part isn't present in the received string as parameter.
  196. //
  197. - (void) _decodeLocal: (NSString *) theString
  198. {
  199.   // If localMailDirectoryPath is nil, we return the last path component
  200.   // of the URL as the foldername.
  201.   if ( !path )
  202.     {
  203.       foldername = [theString lastPathComponent];
  204.       RETAIN(foldername);
  205.       
  206.       path = [theString substringToIndex: ([theString length] - [foldername length])];
  207.       RETAIN(path);
  208.     }
  209.   else
  210.     {
  211.       foldername = [theString substringFromIndex: ([path length] + 1)];
  212.       RETAIN(foldername);
  213.  
  214.       //NSDebugLog(@"|%@| |%@|", path, foldername);
  215.     }
  216. }
  217.  
  218.  
  219. //
  220. // FIXME (finish!)
  221. // pop://<user>;auth=<auth>@<host>:<port>
  222. //
  223. // Examples: pop://rg@mailsrv.qualcomm.com
  224. //           pop://rg;AUTH=+APOP@mail.eudora.com:8110
  225. //           pop://baz;AUTH=SCRAM-MD5@foo.bar
  226. //
  227. // Note: The pop:// part isn't present in the received string as parameter.
  228. //
  229. - (void) _decodePOP3: (NSString *) theString
  230. {
  231.   NSRange aRange;
  232.  
  233.   foldername = [[NSString alloc] initWithString: @"INBOX"];
  234.   
  235.   aRange = [theString rangeOfString: @"@"];
  236.   
  237.   username = [theString substringToIndex: aRange.location];
  238.   RETAIN(username);
  239.  
  240.   host = [theString substringFromIndex: (aRange.location + 1)];
  241.   RETAIN(host);
  242. }
  243.  
  244.  
  245. //
  246. //
  247. //
  248. - (void) _decodeURL: (NSString *) theString
  249. {
  250.   NSRange aRange;
  251.  
  252.   // We first decode our protocol.
  253.   aRange = [theString rangeOfString: @"://"];
  254.   
  255.   if ( aRange.length )
  256.     {
  257.       NSString *aString;
  258.  
  259.       protocol = [theString substringToIndex: aRange.location];
  260.       RETAIN(protocol);
  261.       
  262.       aString = [theString substringFromIndex: (aRange.location + aRange.length)];
  263.  
  264.       if ( [protocol caseInsensitiveCompare: @"LOCAL"] == NSOrderedSame )
  265.     {
  266.       [self _decodeLocal: aString];
  267.     }
  268.       else if ( [protocol caseInsensitiveCompare: @"POP3"] == NSOrderedSame )
  269.     {
  270.       [self _decodePOP3: aString];
  271.     }
  272.       else if ( [protocol caseInsensitiveCompare: @"IMAP"] == NSOrderedSame )
  273.     {
  274.       [self _decodeIMAP: aString];
  275.     }
  276.       else
  277.     {
  278.       NSDebugLog(@"URLName: Malformed URL. Unsupported protocol specified.");
  279.     }
  280.     }
  281.   else
  282.     {
  283.       NSDebugLog(@"URLName: Malformed URL.");
  284.     }
  285. }
  286.  
  287. @end
  288.